/* 
 * Copyright 2009 HIT-CIR (HIT Center for Information Retrieval). 
 * 
 * author: chuter
 * mail:   lliu@ir.hit.edu.cn
 */
package edu.hit.irlab.nlp.ner;

import java.io.IOException;
import java.util.Vector;

import nlpeap.tms.components.NameEntity;
import nlpeap.toolrepository.LoadException;
import nlpeap.toolrepository.NameEntityRecog;
import nlpeap.toolrepository.Tool;

import com.wintim.nlp.ner.TMS_NER;


/**
 * The Name Entity Recognizing tool in Java version<br>
 * input: <br>
 * the vector of the words and their tags
 * output:<br>
 * the vector of the Name Entity tags for the words 
 * 
 * @author chuter
 * @since Corference Resolution Platform
 */
public class NER4j implements NameEntityRecog, Tool {

	//The Name Entity Recognizing tool
	private static TMS_NER ner;
	
	public NER4j() throws IOException {
		if (ner == null)
			ner = new TMS_NER();
	}
	
	/**
	 * Extract the NameEntity tag from the tag of the result of the tool.<br>
	 * The "Tag" is in the form like: "B-Nh", the first character states the
	 * which part of a NameEntity the word is, "B" stands for the beginning,
	 * while "I" stands for the inner part, a single "O" states that the word
	 * is not a part of a NameEntity<br>
	 * What we do is to get the NameEntity tag, it is "Nh" we need for the Tag
	 * "B-Nh" or "I-Nh"
	 * 
	 * @param Tag
	 * 			the Tag that given by the tool
	 * @return
	 * 			the NameEntity tag
	 */
	private String exactNeTag(String Tag) {
		int startPos = Tag.indexOf("-");
		assert(startPos>0 && startPos<Tag.length());
		return Tag.substring(startPos+1);
	}
	
	/**
	 * @see NameEntityRecog#nameEntityRecog(Vector, Vector, Vector)
	 */
	public void nameEntityRecog(Vector<String> word_vec,
		Vector<String> tag_vec, Vector<NameEntity> nameEntity_vec) {
		if (word_vec==null || tag_vec==null || nameEntity_vec==null) {
			System.err.println(this.getClass().getName()
					+"nameEntityRecog: "+"vector is null");
			return;
		}
		
		if (word_vec.size() != tag_vec.size()) {
			System.err.println(this.getClass().getName()+"nameEntityRecog: "
					+"the size of the words and tags are not equal!!");
			return;
		}
		
		// do nothing when the vector is empty
		if (word_vec.size() == 0)
			return;
		
		Vector<String> neTag_vec = new Vector<String>();
		ner.recogfromSeg(word_vec, tag_vec, neTag_vec);
		assert(word_vec.size() == neTag_vec.size());
		
		int position = 0;
		for (int i = 0; i < neTag_vec.size(); i++) {
			if (neTag_vec.get(i).startsWith("B")) {
				String temp = word_vec.get(i);
				String tag = exactNeTag(neTag_vec.get(i));
				for (++i; i<neTag_vec.size()&&neTag_vec.get(i).startsWith("I"); i++)
					temp += word_vec.get(i);
				NameEntity ne = new NameEntity(position, temp, tag);
				nameEntity_vec.add(ne);
				position += temp.length();
			}
			if (i < neTag_vec.size())
				position += word_vec.get(i).length();
		}
	}

	public void loadResource() throws LoadException {
	}

	public void releaseResource() {		
	}

}
